<?php

require_once './config.inc.php';
require_once('./includes/Gettext/gettext.inc');

use Doctrine\Common\ClassLoader;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;

require_once 'includes/Doctrine/Common/ClassLoader.php';

function __autoload($className) {
  
  if(file_exists('./includes/'.$className.'.php')) {
    include './includes/'.$className.'.php';
    
  } else if(file_exists('./includes/Entities/'.$className.'.php')) {
    include './includes/Entities/'.$className.'.php';
      
  } else if(file_exists('./includes/Image/'.$className.'.php')) {
    include './includes/Image/'.$className.'.php';
  }  
}


/*** Database ***/
$DoctrineConfig = new \Doctrine\ORM\Configuration();
$DoctrineMetadata = $DoctrineConfig->newDefaultAnnotationDriver('./includes/Entities');
$DoctrineConfig->setMetadataDriverImpl($DoctrineMetadata);
$DoctrineConfig->setResultCacheImpl(new \Doctrine\Common\Cache\ApcCache());

$DoctrineConfig->setProxyNamespace('proxy');
$DoctrineConfig->setProxyDir('./Proxy');

$db = EntityManager::create($config['DB'], $DoctrineConfig);

/*** User Authentication ***/
$auth = new Authentication($db);

$user = null;
$LOGGED = false;

if(($idUser = $auth->whoIsLogged()) !== null) {
  $user = $db->find('User', $idUser);
  $LOGGED = true;
}  

/*** Setting ***/
$theme = ($LOGGED) ? $user->StyleName : $config["Page"]["Theme"];
  $else = (isset($_SESSION['language'])) ? $_SESSION['language'] : $config["Page"]["Language"];
$locale = ($LOGGED) ? $user->LanguageName : $else;
$title = $config["Page"]["Title"];
$page = (isset($_GET["page"])) ? $_GET["page"] : "index";
$dateFormat = "d.m.Y H:i:s";


/*** L10N ***/
T_setlocale(LC_MESSAGES, $locale);
$domain = 'messages';
T_bindtextdomain($domain, 'locale');
T_bind_textdomain_codeset($domain, $config["Page"]["Charset"]);
T_textdomain($domain);
